﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acms.AcmsControls
{
    [Serializable]
    [System.Xml.Serialization.XmlRoot("File")]
    public class DownloadableFileContentControlProperties : BaseContentControlProperties
    {
        private int resourceId;
        public int ResourceId
        {
            get { return resourceId; }
            set { resourceId = value; }
        }

        public bool HasResource()
        {
            return resourceId > 0;
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Acms.Bll.File.FileTypes FileType{ get; set; }

        public string Extension { get; set; }

        protected override void ReadValue(System.Xml.XmlReader reader)
        {
            if (reader.Name.ToLower() == "resourceid" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    resourceId = reader.ReadContentAsInt();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "name" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    name = reader.ReadContentAsString();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "extension" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    Extension = reader.ReadContentAsString();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "filetype" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    int tmp = reader.ReadContentAsInt();
                    FileType = (Acms.Bll.File.FileTypes)tmp;
                }
                catch
                {
                }
            }
        }

        protected override void WriterPropertiesToXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("content");

            writer.WriteAttributeString("resourceId", resourceId.ToString());
            writer.WriteAttributeString("extension", Extension);
            writer.WriteAttributeString("fileType", ((int)FileType).ToString());
            writer.WriteElementString("name", name);

            writer.WriteEndElement();
        }
    }
}
